<?php
class kurse_controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
		if($this->session->userdata('logged_in'))
   		{
			$this->load->model('kurse_model');
		}
   		else
   		{
     		//If no session, redirect to login page
     		redirect('welcome', 'refresh');
   		}
	}

	public function index()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{
			$data['kurse'] = $this->kurse_model->get_kurse();
	
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('kurse/kurseindex_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function create_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{	
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('kurse/kursecreate_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Code', 'Code', 'required');	
		$this->form_validation->set_rules('Beschreibung', 'Beschreibung', 'required');														   

		if ($this->form_validation->run() === FALSE)
		{
			$this->create_view();
		}
		else
		{
			$this->kurse_model->set_kurse();	
			$this->index();
		}
	}
	
	public function validate_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{
			$kurseid = $this->input->post('kurseid');
			$data['kurse'] = $this->kurse_model->get_kurse_id($kurseid);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('kurse/kursevali_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function validate()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Beschreibung', 'Beschreibung', 'required');														   

		if ($this->form_validation->run() === FALSE)
		{	
			$this->validate_view();
		}
		else
		{
			$kurseid= $this->input->post('Code');
			$this->kurse_model->validate_kurse($kurseid);

			$this->index();
		}	 
	}
	
	public function kurselistview($page = 0)
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{	
			$limit = 5;
			$data['kurse'] = $this->kurse_model->get_all($limit, $page * $limit);
			$data['pages'] = ceil($this->kurse_model->get_count() / $limit);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('kurse/kurselist_view',$data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
}